


class Solution {
    public static void main(String[] args) {
       ListNode l1 = new ListNode(1) ;
       ListNode l2 = new ListNode(2);
       l1.next = l2 ;

       ListNode head = rotateRight(l1,2);


    }
    public static  ListNode rotateRight(ListNode head, int k) {
        ListNode newHead = head ;
        ListNode newEnd = head ;
        int count = 0 ;
        while(newEnd!=null && newEnd.next !=null){
            newEnd = newEnd.next ;
            count ++ ;
        }
        count++ ;
        if(count != 0){
            if(k > count){
                k = (k%count) ;
            }
        }




        for(int i = 0 ; i < k ; i++){
            ListNode cur = head ;
            int num = 0 ;
            while(cur!=null ){
                if(cur==head){
                    num = cur.val ;
                    cur.val = newEnd.val ;
                    cur = cur.next ;
                }else {
                    int tmp = cur.val ;
                    cur.val = num ;
                    num = tmp ;
                    cur =cur.next ;
                }
            }
        }
        return newHead ;



    }
         

          

          for(int i = 0 ; i < k ; i++){
            ListNode cur = head ;
            int num = 0 ;
            while(cur!=null ){
               if(cur==head){
                   num = cur.val ;
                   cur.val = newEnd.val ;
                   cur = cur.next ;
               }else {
                  int tmp = cur.val ;
                 cur.val = num ;
                 num = tmp ;
                 cur =cur.next ;
               } 
            }    
          }
           return newHead ;



    }
}